package com.example.demo.ThreadTest.ThreadConcurrency;

import com.example.demo.RedisTest.config.RedisConstant;
import com.example.demo.RedisTest.config.RedisUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.concurrent.TimeUnit;

/**
 * @author FF998
 */
@Service
@Slf4j
public class ThreadConcurrencyServiceImpl implements ThreadConcurrencyService {

    @Autowired
    private RedisUtils redisUtils;

    @Override
    public String ThreadConcurrencySave() throws InterruptedException {
        String uuid = RedisConstant.UUID;
        String id = ":110";
        // 判断指定key是否存在
        String key = uuid + id;
        String content = "人生的每一个阶段,都会有一个陪伴你的重要伙伴!";
        long timeout = 5;
        if (redisUtils.setValue(key, content)) {

            // 处理代码逻辑
            TimeUnit.SECONDS.sleep(timeout);
            // 代码逻辑处理结束

            redisUtils.removeValue(key);
            return "请求成功!!!";
        } else {
            return "接口已被请求,请勿重复操作!";
        }

    }

}
